package com.lgd.warehousemanagement.httpService;

import android.util.Log;

import com.lgd.warehousemanagement.httpService.httpPort.IResponse;
import com.lgd.warehousemanagement.httpService.httpPort.IStringResponse;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;


public class TsHttp {
    private  static TsHttp tsHttp;
    private OkHttpClient okHttpClient;

    public static TsHttp getInstance(){
        if(null==tsHttp){
            synchronized (TsHttp.class){
                if(null==tsHttp){
                    tsHttp=new TsHttp();
                    OkHttpClient okHttpClient = new OkHttpClient.Builder()
                            .connectTimeout(5, TimeUnit.SECONDS)
                            .readTimeout(5, TimeUnit.SECONDS)
                            .build();
                    tsHttp.setOkHttpClient(okHttpClient);
                }
            }
        }
        return  tsHttp;
    }

    public void get(String url,final IStringResponse myResponse){
        try{
            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8;");
            final Request request = new Request.Builder()
                    .url(url)
                    .build();
//             url= URLEncoder.encode(url,"utf-8");
            System.out.println(url+"------888888888");
            Call call = this.okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    myResponse.onFail(0);
                }

                @Override
                public void onResponse(Call call, Response response) {

                    if (response.isSuccessful()) {
                        String result = null;
                        try {
                            result = response.body().string();
                        } catch (IOException e) {

                        }
                        myResponse.onSuccess(result);
                    }else{
                        myResponse.onFail(response.code());
                    }
                }
            });
        }catch (Exception e) {
            myResponse.onFail(-1);
            e.printStackTrace();
        }
    }

    public void get(String url,final IResponse myResponse){
        try{
            MediaType mediaType = MediaType.parse("application/x-www-form-urlencoded; charset=utf-8;");
            final Request request = new Request.Builder()
                    .url(url)
                    .get()
                    .build();
            // url= URLEncoder.encode(url,"utf-8");
            System.out.println(url+"7777777777777");
            OkHttpClient okHttpClient2 = new OkHttpClient();
            Call call = okHttpClient2.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    myResponse.onFail(-2);
                    System.out.println("--errrrrrrrrr------"+e);
                }
                @Override
                public void onResponse(Call call, Response response)  {
                    System.out.println("*****************"+response);
                    if (response.isSuccessful()) {
                        String result = null;
                        try {
                            result = response.body().string();
                            myResponse.onSuccess(result);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }

                    }else{
                        myResponse.onFail(response.code());
                    }

                }
            });
        }catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void post(final String url, final String body, final IStringResponse myResponse){
        try{
            // url= URLEncoder.encode(url,"utf-8");
            System.out.println("----file -----" + body);
            RequestBody req = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                    , body);
            Request request = new Request.Builder()
                    .url(url)
                    .post(req)
                    .build();
            System.out.println(req+"-----------------post请求--------路径  ****-----------");
            OkHttpClient okHttpClient = new OkHttpClient();
            Call call =okHttpClient.newCall(request);
            call.enqueue(new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    myResponse.onFail(0);

                    Log.d("RequestFF","请求失败" + url);
                }
                @Override
                public void onResponse(Call call, Response response) {
                    if (response.isSuccessful()) {
                        String result = null;
                        try {
                            result = response.body().string();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        myResponse.onSuccess(result);
                    }else{
                        myResponse.onFail(response.code());
                    }
                }
            });
        }catch (Exception e){
            myResponse.onFail(-1);
            e.printStackTrace();
            System.out.println("----错误-------"+e);
        }
    }

    public static TsHttp getTsHttp() {
        return tsHttp;
    }

    public static void setTsHttp(TsHttp tsHttp) {
        TsHttp.tsHttp = tsHttp;
    }

    public OkHttpClient getOkHttpClient() {
        return okHttpClient;
    }

    public void setOkHttpClient(OkHttpClient okHttpClient) {
        this.okHttpClient = okHttpClient;
    }

    @Override
    public String toString() {
        return "TsHttp{" +
                "okHttpClient=" + okHttpClient +
                '}';
    }

}
